package com.lhyl.front.web.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lhyl.front.common.SiteUtil;
import com.lhyl.front.common.StringUtils;
import com.lhyl.front.constant.FrontCheckConstant;
import com.lhyl.front.constant.PayConstant;
import com.lhyl.front.constant.UserConstant;
import com.lhyl.front.exception.FrontException;
import com.lhyl.front.web.controller.BaseController;
import com.lhyl.front.web.shiro.model.User;
import com.lhyl.front.web.shiro.service.PasswordHelper;
import com.lhyl.front.web.user.model.UserCash;
import com.lhyl.front.web.user.model.UserCharge;
import com.lhyl.front.web.user.service.UserCashService;
import com.lhyl.front.web.user.service.UserChargeService;

/**
 * 银行充提
 * @author GusonYang
 *
 */
@Controller
public class BankController extends BaseController{

	@Autowired
	public UserCashService userCashService;
	  
	@Autowired
	public UserChargeService userChargeService;
	
	@Autowired
    private PasswordHelper passwordHelper;
	
    
    @RequestMapping("/bank/recharge")
    public String Recharge(ModelMap modelMap)  throws Exception{
    	List<Map> bankList = reportService.getReportResult("queryBankList", null);
    	modelMap.put("bankList", bankList);
        return "bank/recharge";
    }
    
    @RequestMapping("/content/bank/recharge")
    public String ContentRecharge(ModelMap modelMap)  throws Exception{
    	List<Map> bankList = reportService.getReportResult("queryBankList", null);
    	modelMap.put("bankList", bankList);
        return "bank/content-recharge";
    }
    
    @RequestMapping("/bank/tocash")
    public String Tocash(ModelMap modelMap)  throws Exception{
    	super.initBaseData(modelMap);
        return "bank/tocash";
    }
    
    public Integer getTodayTocashCount(String uid) throws Exception{
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("uid", uid);
    	List list = reportService.getReportResult("getTodayTocashCount", param);
    	Map map = (Map)list.get(0);
    	return Integer.valueOf(map.get("count")+"");
    }
    
    public Map getTodayChargeBetsAmount(String uid) throws Exception{
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("uid", uid);
    	List list = reportService.getReportResult("getChargeAndBetsAccount", param);
    	Map map = (Map)list.get(0);
    	double bets_amount = Double.valueOf(map.get("bets_amount")+"");
    	double charge_amount = Double.valueOf(map.get("charge_amount")+"");
    	if(charge_amount == 0){
    		map.put("expend_ratio", 100);
    	}else{
    		map.put("expend_ratio", bets_amount/charge_amount * 100);
    	}
    	return map;
    }
    
    @RequestMapping("/content/bank/tocash")
    public String ContentTocash(ModelMap modelMap)  throws Exception{
    	Map map = new HashMap<String, Object>();
    	User user = getLoginUserObject();
    	map.put("uid", user.getUid());
    	List bankList =reportService.getReportResult("queryUserBanks", map);
    	if(bankList==null||bankList.isEmpty())
    	{
    		 modelMap.put("errorMsg", "Y");
    		 return "bank/content-tocash";
    	}
		modelMap.put("user", user);
    	modelMap.put("bankList", bankList);//获取会员绑定的默认银行信息 
    	modelMap.put("todayTocashCount", getTodayTocashCount(user.getUid()));
    	modelMap.put("chargeBetsAmount", getTodayChargeBetsAmount(user.getUid()));
        return "bank/content-tocash";
    }
     
    
    @RequestMapping(value="/bank/tocash_submit" , method=RequestMethod.POST)
    public @ResponseBody Map TocashSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
    	Map map = new HashMap();
    	String bank_id =req.getParameter("bank_id");
		String amount = req.getParameter("amount");
		String password = req.getParameter("password");
        User user = getLoginUserObject();
        if(user.getVirtualAccount()==UserConstant.VIRTUAL_ACCOUNT){
        	map.put("success", false);
        	map.put("message", "虚拟账户不能提现");
        	return map;
        }
        if(Integer.valueOf(amount) > user.getCoin()){
        	map.put("success", false);
        	map.put("message", "提现金额不能大于账户余额");
        	return map;
        }
        String ep = passwordHelper.encryptPassword(password);
        if(!ep.equals(user.getCoinPassword())){
        	map.put("success", false);
        	map.put("message", "资金密码错误");
        	return map;
        }
        int count = getTodayTocashCount(user.getUid());
        if(count >= 3){
        	map.put("success", false);
        	map.put("message", "每天提现次数不能超过3次");
        	return map;
        }
        Map chargeBetsAmount = getTodayChargeBetsAmount(user.getUid());
        if((StringUtils.str2Double(chargeBetsAmount.get("expend_ratio")+"")) < 30){
        	map.put("success", false);
        	map.put("message", "消费比例未达到系统设置的30%");
        	return map;
        }
        Map<String,Object>param = new HashMap<String, Object>();
        param.put("uid", user.getUid());
        try
        {
          boolean bol = reportService.verification(FrontCheckConstant.ACTIVITY_COMMISSION_CHECK, param);
          if(!bol)
          {
        		map.put("success", false);
            	map.put("message", "活动佣金校验失败。"); 
            	return map;
          }
        }catch (FrontException e) {
        	map.put("success", false);
        	map.put("message", e.getMessage());
        	map.put("errorCode", e.getErrorCode());
        	return map;
		}
        UserCash userCash = new UserCash();
        userCash.setUid(user.getUid());
        userCash.setUname(user.getLoginName());
        userCash.setApplyTime(new Date()); 
        userCash.setBankName(req.getParameter("bankName"));
        userCash.setBankAccount(req.getParameter("account"));
        userCash.setBankUsername(req.getParameter("uname"));
        userCash.setDepositBank(req.getParameter("openingName"));
        userCash.setAmount(Integer.valueOf(amount));
        userCash.setStatus(-1);
       
        userCashService.insert(userCash);
    
        map.put("success", true);
        return map;
    }
    
    @RequestMapping(value="/bank/recharge_submit" , method=RequestMethod.POST)
    public @ResponseBody UserCharge rechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
    	String bank_id =req.getParameter("bank_id");
    	String amount = req.getParameter("amount");
    	String onlinePayment=req.getParameter("onlinePayment");
        User user = getLoginUserObject();
        
        UserCharge userCharge = new UserCharge();
        userCharge.setChargeType(StringUtils.equals(onlinePayment, "Y")?PayConstant.CHARGE_TYPE_ONLINE:PayConstant.CHARGE_TYPE_OFFLINE);
        userCharge.setUid(user.getUid());
        userCharge.setUname(user.getLoginName());
        userCharge.setAccount(Double.valueOf(amount));
        userCharge.setStatus("-1");
        userCharge.setRemark("用户充值");
        userCharge.setChargeDate(new Date());
        userCharge.setChargeIp(SiteUtil.getIpAddr());
        userCharge.setChargeAccount(Integer.valueOf(bank_id));
        userCharge.setIsDelete(0);
        
        userChargeService.insert(userCharge);
        
        return userCharge;
    }
    
    @RequestMapping(value="/content/bank/recharge_submit" , method=RequestMethod.GET)
    public String ContentRechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
    	String charge_id = req.getParameter("charge_id");
        Map<String, Object>map = new HashMap<String, Object>();
        map.put("id", charge_id);
        modelMap.put("charge", this.reportService.querySingleResult("queryChargeDetail", map));
        return "bank/content-recharge_submit";
    }
    
    @RequestMapping(value="/content/bank/online_recharge" , method=RequestMethod.GET)
    public String ContentOnlineRechargeSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
    	String charge_id = req.getParameter("charge_id");
        Map<String, Object>map = new HashMap<String, Object>();
        map.put("id", charge_id);
        modelMap.put("charge", this.reportService.querySingleResult("queryChargeDetail", map));
        return "bank/content-recharge-online";
    }
    
    @RequestMapping(value="/content/bank/tocash_submit" , method=RequestMethod.GET)
    public String ContentTocashSubmit(HttpServletRequest req,HttpServletResponse resp, ModelMap modelMap)  throws Exception{
        return "bank/content-tocash_submit";
    }
}
